import { ref } from 'vue'
import axios from '../plugins/axios'
import { closeToast, showLoadingToast } from 'vant'

export default () => {
   const articelList = ref<ResponseTypes<ArticelModel[]>>()
   const getArticelList = async (categoryId = 0, page = 1, userId?: number) => {
      console.log(userId)
      articelList.value = await axios.request({
         url: `articel/getAll/${categoryId}/${page}/${userId}`
      })
   }

   const categoryList = ref<ResponseTypes<CategoryModel[]>>()
   const getCategoryList = async () => {
      categoryList.value = await axios.request<CategoryModel[]>({ url: 'category' })
   }

   interface model {
      title: string
      content: any
      previewImg: string
      categoryId: number
   }
   const addArticle = async (articel: model) => {
      await axios.request<ArticelModel>({ url: 'articel', data: articel, method: 'post' })
   }

   const articel = ref<ArticelModel>()
   const getOneArticle = async (id: number) => {
      showLoadingToast({
         message: '加载中...',
         forbidClick: true
      })
      const res = await axios.request<ArticelModel>({
         url: `articel/${id}`
      })
      articel.value = res.data
      closeToast()
   }

   const deleArticle = async (id: number) => {
      return await axios.request({
         url: `articel/${id}`,
         method: 'Delete'
      })
   }

   return {
      getArticelList,
      articelList,
      getCategoryList,
      categoryList,
      addArticle,
      getOneArticle,
      articel,
      deleArticle
   }
}
